/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Validations;

import com.ControlEscolar.Beans.Exam;

import com.ControlEscolar.DAOs.DAOException;
import com.ControlEscolar.DAOs.ExamDAO;
import com.ControlEscolar.Services.ReticleService;
import java.util.ArrayList;

/**
 *
 * @author Bere
 */
public class ValidationExam extends BasicValidation{

    ExamDAO examDAO= new ExamDAO();
    ReticleService reticleService=new ReticleService();

    public boolean isExistExam(Exam exam) throws DAOException{
      Exam searchExam=examDAO.queryExam(exam);
      return isFound(searchExam);
    }

    public boolean isMaxExtraAssigned(String subjectKey,String studentKey) throws DAOException{
       final int MAX_EXTRAORDINARY = 3;
       ArrayList<Exam> searchExtra=examDAO.queryExtras(subjectKey, studentKey);
       int numExtras=searchExtra.size();
       if(numExtras==MAX_EXTRAORDINARY)
            return true;
       else
            return false;
   }

   public boolean isMaxOrdinaryAssigned(String studentKey,String subjectKey) throws DAOException{
      final int MAX_ORDINARY = 2;
      ArrayList<Exam> searchOrdinary=examDAO.queryOrdinary(subjectKey, studentKey);
      int numOrdinarys=searchOrdinary.size();
      if(numOrdinarys==MAX_ORDINARY)
        return false;
      else
        return true;
      }



   public boolean isFailedMaxExtra( String studentKey, int ID_Curriculum, int semester) throws DAOException{
       final int MAX_FAILED_EXTRA=3;
       ArrayList<String> subjectsKeys=reticleService.getSubjectsToStudent(ID_Curriculum, semester);
       ArrayList<Exam> searchFailedExtra=new ArrayList();
       for(int index=0; index<subjectsKeys.size();index++){
                searchFailedExtra = examDAO.queryFailedExtras(subjectsKeys.get(index), studentKey);
                int numFailedExtra=searchFailedExtra.size();
                if (numFailedExtra == MAX_FAILED_EXTRA) {
                    return true;
                }
       }
       return false;
   }

    public boolean examWithoutGradeOrFailed(String subjectKey, String studentKey) throws DAOException {
        final int MIN_GRADE = 60;
        Exam lastExam = examDAO.queryLastExam(subjectKey, studentKey);

        if (lastExam == null) {
            return true;
        } else {
            return lastExam.getExamGrade() < MIN_GRADE;
        }
    }
}
